﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using Gizmox.WebGUI.Common.Interfaces;
using System.Web.UI.WebControls;
using System.Data;
using System.Text;
using System.IO;

namespace BusinessNuke.ImportBusiness.Web.Categories.Business
{
    public class ExportToExcelGatewayHandler : IGatewayHandler
    {
        private DataTable exportData;

        public HttpContext context
        {
            get
            {
                return HttpContext.Current;
            }
        }
        #region IGatewayHandler Members

        public ExportToExcelGatewayHandler(DataTable data)
        {
            exportData = data;
        }

        public void ProcessGatewayRequest(IContext objContext, IRegisteredComponent objComponent)
        {
            context.Response.AddHeader("Content-Disposition", "attachment; filename=出口导出.xls");
            context.Response.ContentType = "application/vnd.ms-excel";
            context.Response.ContentEncoding = System.Text.Encoding.GetEncoding("GB2312");//设置输出流为简体中文
            context.Response.Charset = "";

            StringBuilder stringBuilder = new StringBuilder();

            GridView gridView = new GridView();
            gridView.DataSource = exportData;
            gridView.DataBind();

            foreach (TableCell headerCell in gridView.HeaderRow.Cells)
            {
                stringBuilder.Append(headerCell.Text);
                stringBuilder.Append("\t");
            }

            //    if (headerCell.Text == "BackToCustomerDate")
            //        stringBuilder.Append("送还客户时间");
            //    if (headerCell.Text == "DrawbackedDate")
            //        stringBuilder.Append("退单时间");
            //    //headerCell.Text = "退单时间";
            //    if (headerCell.Text == "DeclaredDate")
            //        stringBuilder.Append("报关完成时间");
            //    //headerCell.Text = "报关完成时间";
            //    if (headerCell.Text == "SentDate")
            //        stringBuilder.Append("送单时间");
            //    //headerCell.Text = "送单时间";
            //    if (headerCell.Text == "ReceivedDate")
            //        stringBuilder.Append("接单时间");
            //    //headerCell.Text = "接单时间";
            //    if (headerCell.Text == "ExpressCompanyName")
            //        stringBuilder.Append("快递公司");
            //    //headerCell.Text = "快递公司";
            //    if (headerCell.Text == "CustomCompanyName")
            //        stringBuilder.Append("报关公司");
            //    //headerCell.Text = "报关公司";
            //    if (headerCell.Text == "OrderCompanyName")
            //        stringBuilder.Append("订仓公司");
            //    //headerCell.Text = "订仓公司";
            //    if (headerCell.Text == "OperationName")
            //        stringBuilder.Append("经营单位");
            //    //headerCell.Text = "经营单位";
            //    if (headerCell.Text == "DeclarationNumber")
            //        stringBuilder.Append("海关编号");
            //    //headerCell.Text = "海关编号";
            //    if (headerCell.Text == "ApproveNumber")
            //        stringBuilder.Append("核销单号");
            //    //headerCell.Text = "核销单号";
            //    if (headerCell.Text == "BillNumber")
            //        stringBuilder.Append("提运单号");
            //    //headerCell.Text = "提运单号";
            //    if (headerCell.Text == "OrderStatusName")
            //        stringBuilder.Append("订单状态");
            //    //headerCell.Text = "订单状态";
            //    if (headerCell.Text == "Conveyance")
            //        stringBuilder.Append("船名航次");
            //    //headerCell.Text = "船名航次";
            //    if (headerCell.Text == "OrderNumber")
            //        stringBuilder.Append("订仓单号");
            //    if (headerCell.Text == "Note")
            //        stringBuilder.Append("备注");
            //    //headerCell.Text = "订仓单号";
            //    stringBuilder.Append("\t");
            //}

            stringBuilder.Append("\r\n");

            foreach (GridViewRow row in gridView.Rows)
            {
                foreach (TableCell cell in row.Cells)
                {
                    if (cell.Text == "&nbsp;")
                    {
                        cell.Text = "";
                    }
                    if (cell.Text == "1990/1/1 0:00:00")
                        cell.Text = "";
                    stringBuilder.Append(cell.Text + "\t");
                }
                stringBuilder.Append("\r\n");
            }

            context.Response.Clear();
            context.Response.Buffer = true;

            System.IO.StringWriter stringWriter = new StringWriter(stringBuilder);
            //HtmlTextWriter htmlTextWriter = new HtmlTextWriter(stringWriter, "");
            //gridView.RenderControl(htmlTextWriter);

            //context.Response.Write(stringWriter.ToString());
            context.Response.Write(stringBuilder.ToString());
            context.Response.End();
        }

        #endregion
    }
}